package datasender;

import java.util.ArrayList;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.Message;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.json.JSONArray;
import org.json.JSONObject;

import com.fasterxml.jackson.databind.ObjectMapper;

import pojo.Book;
import util.FileProcessDataUtil;

public class PTPProductor {
		public static void main(String[] args) throws Exception{
			ConnectionFactory factory=new ActiveMQConnectionFactory("tcp://localhost:61616");
			Destination queue=new ActiveMQQueue("evan01");
			Connection con=factory.createConnection();
			con.start();
			Session sen=con.createSession(true, Session.AUTO_ACKNOWLEDGE);
			MessageProducer producer=sen.createProducer(queue);
			ArrayList<Book> bookList = FileProcessDataUtil.readData();
			for(Book book:bookList){
				ObjectMapper mapper = new ObjectMapper();
				String bookStr = mapper.writeValueAsString(book);
				System.out.println(bookStr);
				TextMessage msg1=sen.createTextMessage(bookStr);
				producer.send(msg1);
				sen.commit();
			}
			producer.close();
			sen.close();
			con.close();
		}
}
